class MyButton extends HTMLElement {
  static style = `
  @import url("http://at.alicdn.com/t/c/font_3828430_140r7z3i943g.css");

  .my-button {
    background-color: #fff;
    outline: none;
    border: none;
    cursor: pointer;
    padding: 5px 20px;
    border-radius: 5px;
    border: 1px solid #ccc;
    overflow: hidden;
    box-shadow: 4px 8px 19px -3px rgba(0, 0, 0, 0.27);
    position: relative;
    z-index: 1;
    transition: all 250ms;
  }

  .my-button .iconfont {
    vertical-align: bottom;
  }

  .my-button::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0;
    background-color: #ee7419;
    /* 记得换成变量 */
    z-index: -1;
    -webkit-box-shadow: 4px 8px 19px -3px rgba(0, 0, 0, 0.27);
    box-shadow: 4px 8px 19px -3px rgba(0, 0, 0, 0.27);
    transition: all 250ms;
  }

  .my-button:hover {
    color: #fff;
    border-color: #d6630b;
  }

  .my-button:hover::before {
    width: 100%;
  }

  .my-button:active {
    transform: translateY(2px);
  }

  .primary {
    background-color: #ee7419;
    color: #fff;
  }

  .primary:hover {
    background-color: #f18431;
  }

  .primary::before {
    display: none;
  }

  .larger {
    padding: 10px 30px;
    font-size: 20px;
  }

  .larger .iconfont {
    font-size: 25px;
  }
  `

  constructor() {
    super()
  }

  connectedCallback() {
    let shadow = this.attachShadow({ mode: "open" })
    let _data = {
      icon: this.getAttribute("icon") || "",
      text: this.innerHTML || "",
      type: this.getAttribute("type") || "",
      size: this.getAttribute("size") || "",
    }
    this.render(shadow, _data)
  }

  render(shadow, data) {
    let style = document.createElement("style")
    style.innerHTML = MyButton.style

    shadow.innerHTML = `
      <button class="my-button ${data.type} ${data.size}">
        <i class="iconfont ${data.icon}"></i>
        ${data.text}
      </button>
    `

    shadow.appendChild(style)
  }
}

customElements.define("my-button", MyButton)

export default MyButton
